//
//  GHSOD_WLViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/16.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GHSOD_WLViewController.h"
#import "GHSOD_SectionHeaderView.h"
#import "GHSOD_GoodsTableViewCell.h"
#import "GHSOD_BaseTableViewCell.h"

@interface GHSOD_WLViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray<SPSection *> *sections;
@property (nonatomic,weak) UITableView *tableView;

@end

@implementation GHSOD_WLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"订单详情";
    [self initDataSource];
    [self setupTableView];
}


- (void)initDataSource
{
    NSArray *items = @[@"商品信息",@"买家信息",@"物流信息",@"订单信息"];
    [items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SPSection *sec = [SPSection new];
        sec.items = [NSMutableArray array];
        sec.title = obj;
        sec.open = YES;
        [self.sections addObject:sec];
        switch (idx) {
            case 0:{
                [sec.items addObject:@"商品信息"];
                break;
            }
            case 1:{
                [sec.items addObjectsFromArray:@[@"姓名",@"电话",@"地址"]];
                break;
            }
            case 2:{
                [sec.items addObjectsFromArray:@[@"状态",@"快递号"]];
                break;
            }
            case 3:{
                [sec.items addObjectsFromArray:@[@"订单编号",@"付款时间",@"发货时间",@"收货时间"]];
                break;
            }
        }
        
    }];
}



- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView =tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    
    //底部按钮
    UIView *footerV = [UIView new];
    footerV.backgroundColor = [UIColor whiteColor];
    footerV.height = 110;
    UIButton *btn = [UIButton new];
    [btn setTitle:@"查看物流" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    [btn setTitleColor:RZ_COLOR(96, 59, 187) forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"按钮2"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(lookLogisticsInfo) forControlEvents:UIControlEventTouchUpInside];
    [footerV addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(44);
    }];
    self.tableView.tableFooterView = footerV;
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_SectionHeaderView class]) bundle:nil]  forHeaderFooterViewReuseIdentifier:NSStringFromClass([GHSOD_SectionHeaderView class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_GoodsTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSOD_GoodsTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_BaseTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSOD_BaseTableViewCell class])];
    
}


#pragma mark--UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sections.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SPSection *sec = self.sections[section];
    return sec.items.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0;
    if(indexPath.section == 0){
        height = 160;
    }else{
        height = 60;
    }
    return height;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SPSection *sec = _sections[section];
    GHSOD_SectionHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([GHSOD_SectionHeaderView class])];
    headerV.title.text = sec.title;
    headerV.arrow.hidden = YES;
    return headerV;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPSection *sec = self.sections[indexPath.section];
    UITableViewCell *cell;
    if(indexPath.section == 0){
        GHSOD_GoodsTableViewCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSOD_GoodsTableViewCell class])];
        cell = goodsCell;
        
    }else{
        
        GHSOD_BaseTableViewCell *baseCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSOD_BaseTableViewCell class])];
        cell = baseCell;
        //重置数据
        [baseCell.rightBtn setTitle:@"" forState:UIControlStateNormal];
        [baseCell.rightBtn setImage:nil forState:UIControlStateNormal];
        
        
        if(indexPath.row < sec.items.count){
            baseCell.title.text = sec.items[indexPath.row];
        }
        if(indexPath.section == 1 && indexPath.row == 0){//姓名--联系用户
            [baseCell.rightBtn setImage:[UIImage imageNamed:@"icon_ghs_contact"] forState:UIControlStateNormal];
            baseCell.rightBtnOnClickBlock = ^{
                RZLog(@"姓名--联系用户");
            };
        }
        if(indexPath.section == 3 && indexPath.row == 0){//订单编号--复制
            [baseCell.rightBtn setTitle:@"复制" forState:UIControlStateNormal];
            baseCell.rightBtnOnClickBlock = ^{
                RZLog(@"订单编号--复制");
            };
        }
        
    }
    
    return cell;
}



- (void)lookLogisticsInfo
{
    RZLog(@"查看物流");
}




#pragma mark--setter&&getter

- (NSMutableArray<SPSection *> *)sections
{
    if(!_sections){
        _sections = [NSMutableArray array];
    }
    return _sections;
}
@end
